﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace libmorph
{
    public class Mask : GrayscaleImage
    {
        public Mask(int width, int height)
        {
            this.m_imageData = new float[width, height];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    this.m_imageData[x,y] = 0.0f;
                }
            }
        }

        public Mask(float[,] values)
        {
            this.m_imageData = values;
        }

        public static Mask SquareMask(int size)
        {
            Mask returnMask = new Mask(size, size);

            for(int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    returnMask.m_imageData[i, j] = 1.0f;
                }
            }

            return returnMask;
        }

        public static Mask CircleMask(int radius)
        {
            Mask returnMask = new Mask(radius, radius);

            for (int y = 0; y < radius; y++)
            {
                for (int x = 0; x < radius; x++)
                {
                    float radiusHalf = ((float)radius) / 2;
                    float distance = (float)Math.Sqrt((double)(((x - radiusHalf) * (x - radiusHalf)) + ((y - radiusHalf) * (y - radiusHalf))));
                    returnMask.m_imageData[x, y] = (distance <= radiusHalf ? 1.0f : 0.0f);
                }
            }

            return returnMask;
        }

        public static Mask CrossMask(int size)
        {
            Mask returnMask = new Mask(size, size);
            int width = size / 3;

            for (int i = 0; i < size; i++)
            {
                for (int j = -width/2; j <= width/2; j++)
                {
                    returnMask.m_imageData[i, size / 2 + j] = 1.0f;
                    returnMask.m_imageData[size / 2 + j, i] = 1.0f;
                }
            }

            return returnMask;
        }
    }
}
